import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class test4 {
    //力扣 139.单词拆分
    public boolean wordBreak(String s, List<String> wordDict) {
        //创建dp表
        //初始化
        //填表
        //返回值

        //小优化
        Set<String> set = new HashSet<>(wordDict);
        int n = s.length();
        boolean[] dp = new boolean[n+1];
        dp[0] = true;
        s = " " + s;
        for(int i =1;i<n+1;i++){
            for(int j = i;j>=1;j--){
                if(dp[j-1] && set.contains(s.substring(j,i+1))){
                    dp[i] = true;
                    //小优化
                    break;
                }
            }
        }
        return dp[n];
    }
}
